/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrearReclamo.java
 *
 * Created on 08-jul-2012, 17:09:14
 */
package sistemadeproyectos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author giancarlo
 */
public class CrearReclamo extends javax.swing.JFrame {

    /*Usuario Logeado*/
    private Usuario usuario;
    
    /*Equipo al que esta conectado el Usuario*/
    private Equipo equipo;

    /*Modelo del combo box*/
    private DefaultComboBoxModel<String> modelo = new DefaultComboBoxModel();
    
    /** Creates new form CrearReclamo */
    public CrearReclamo(Usuario user_act, Equipo equip_act) {
        initComponents();
        this.usuario = user_act;
        this.equipo = equip_act;
        jLabelAutor.setText((this.usuario).getUsuario());
        this.getRootPane().setDefaultButton(jButtonAgregar);
        jtxtAsunto.requestFocus();
        tipo_reclamo.setModel(modelo);
        modelo.addElement("Remocion de Usuario");
        modelo.addElement("Remocion de Archivo");        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextContenido = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jButtonAgregar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtxtAsunto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabelAutor = new javax.swing.JLabel();
        tipo_reclamo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemadeproyectos.SistemadeproyectosApp.class).getContext().getResourceMap(CrearReclamo.class);
        jButtonCancelar.setText(resourceMap.getString("jButtonCancelar.text")); // NOI18N
        jButtonCancelar.setName("jButtonCancelar"); // NOI18N
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextContenido.setColumns(20);
        jTextContenido.setLineWrap(true);
        jTextContenido.setRows(5);
        jTextContenido.setWrapStyleWord(true);
        jTextContenido.setName("jTextContenido"); // NOI18N
        jScrollPane1.setViewportView(jTextContenido);

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jButtonAgregar.setText(resourceMap.getString("jButtonAgregar.text")); // NOI18N
        jButtonAgregar.setName("jButtonAgregar"); // NOI18N
        jButtonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAgregarActionPerformed(evt);
            }
        });

        jLabel8.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jtxtAsunto.setName("jtxtAsunto"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel2.setInheritsPopupMenu(false);
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabelAutor.setText(resourceMap.getString("jLabelAutor.text")); // NOI18N
        jLabelAutor.setName("jLabelAutor"); // NOI18N

        tipo_reclamo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        tipo_reclamo.setName("tipo_reclamo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(143, 143, 143)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tipo_reclamo, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButtonAgregar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonCancelar)
                            .addGap(11, 11, 11))
                        .addComponent(jLabel2)))
                .addContainerGap(152, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jtxtAsunto, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                                    .addComponent(jLabelAutor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(50, 50, 50)
                .addComponent(tipo_reclamo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAgregar)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(56, 56, 56)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jLabelAutor))
                    .addGap(18, 18, 18)
                    .addComponent(jLabel8)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jtxtAsunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(11, 11, 11)
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(61, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAgregarActionPerformed
        // TODO add your handling code here:
        String texto = "";
        if(jtxtAsunto.getText().equals("")){
            JOptionPane.showMessageDialog(null, "El asunto no puede ser vacio", "Datos incompletos", JOptionPane.ERROR_MESSAGE);
            jtxtAsunto.requestFocus();
        }
        else {
            try { texto = jTextContenido.getText(); }
            catch (NullPointerException e) 
            {
                JOptionPane.showMessageDialog(null, "El contenido no puede ser vacio", "Datos incompletos", JOptionPane.ERROR_MESSAGE);
                jTextContenido.requestFocus();
            }
            if (!(texto.equals(""))) {
                try{
                    BDAdmin motor = new BDAdmin("SELECT * FROM RECLAMOS WHERE ASUNTO = \'" 
                            + jtxtAsunto.getText() + "\'" + "AND AUTOR = \'" + (this.jLabelAutor) +"\'");
                    ResultSet rs = motor.consulta();
                    if(rs.next() == true){
                        JOptionPane.showMessageDialog(this, "Ya tiene registrada una queja con ese asunto", 
                            "Queja Repetida", JOptionPane.ERROR_MESSAGE);
                    }
                    else{
                        /* Seteamos el tipo del reclamo
                         * 0 para Remocion de Usuario
                         * 1 para Remocion de Archivo
                         */
                        int tipo = 0;
                        if ((tipo_reclamo.getSelectedItem()).equals("Remocion de Archivo")) {
                            tipo = 1;
                        }
                        
                        /*Obtenemos la fecha y la hora*/
                        java.util.Date d = new java.util.Date();
                        Calendar c = new GregorianCalendar();
                        c.setTime(d);

                        String fecha = (Integer.toString(c.get(Calendar.DATE)) +
                                "/" + Integer.toString(c.get(Calendar.MONTH)) +
                                "/" + Integer.toString(c.get(Calendar.YEAR)));
                      
                        String hora = (Integer.toString(c.get(Calendar.HOUR_OF_DAY)) +
                            ":" + Integer.toString(c.get(Calendar.MINUTE)) +
                            ":" + Integer.toString(c.get(Calendar.SECOND)));
                        
                        /*Insertamos el reclamo en la base de datos*/
                        String Query = ("INSERT INTO RECLAMOS VALUES('" + jLabelAutor.getText() 
                                + "','" + jtxtAsunto.getText() + "','" + fecha 
                                + "','" + hora + "','" + jTextContenido.getText() + 
                                "'," + tipo + ")");
                        
                        motor.setQuery(Query);                        
                        motor.actualizar();
                        JOptionPane.showMessageDialog(this, "La queja \"" + jtxtAsunto.getText() 
                                + "\" ha sido creada correctamente!","Queja creado", 
                                JOptionPane.INFORMATION_MESSAGE);
                        ((VerHilosForo)this.getParent()).listarEntradas();
                        
                        /*Reiniciamos los campos*/
                        jtxtAsunto.setText("");
                        jTextContenido.setText("");
                        
                        this.dispose();
                    }
                    
                    motor.cerrar();
                }
                catch(SQLException sqle){
                    sqle.printStackTrace();
                }                
            }
            else {
                JOptionPane.showMessageDialog(null, "El contenido no puede ser vacio", "Datos incompletos", JOptionPane.ERROR_MESSAGE);
                jTextContenido.requestFocus();   
            }
        }
    }//GEN-LAST:event_jButtonAgregarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(CrearReclamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(CrearReclamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(CrearReclamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(CrearReclamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new CrearReclamo().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAgregar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabelAutor;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextContenido;
    private javax.swing.JTextField jtxtAsunto;
    private javax.swing.JComboBox tipo_reclamo;
    // End of variables declaration//GEN-END:variables
}
